import zh from '@/assets/lang/zh'
import en from '@/assets/lang/en'

export function getPages() {
	return getCurrentPages();
}

export function getCurrentPage() {
	const pages = getPages()
	const currentPage = pages[pages.length - 1];
	return currentPage
}

export function getPrevPage() {
	const pages = getPages()
	const prevPage = pages[pages.length - 2];
	return prevPage
}

let count = 0;
export function incr() {
	count = count + 1
	return count
}

export function copy(that, text) {
	// #ifdef H5
	clipboard.handleClipboard(text, event, () => {
		showToast(that.$t('copy') + that.$t('success'))
	})
	// #endif
	// #ifndef H5
	uni.setClipboardData({
		data: text,
		success: () => {
			showToast(that.$t('copy') + that.$t('success'))
		}
	})
	// #endif
}

export function listDeviceStatus(that) {
	return [{
			label: that.$t('all') + that.$t('status'),
			value: -1
		},
		{
			// 'ONLINE'
			label: that.$t('ONLINE'),
			value: 1
		},
		{
			// 'OFFLINE'
			label: that.$t('OFFLINE'),
			value: 0
		},
	]
}

export function formatDeviceStatus(that, status) {
	// if (!status) {
	// 	return that.$t('all')
	// }
	const list = listDeviceStatus(that)
	for (const item of list) {
		if (item.value === status) {
			return item.label
		}
	}
}

/**
* 参数处理
* @param {*} params  参数
*/
export function tansParams(params) {
  let result = ''
  for (const propName of Object.keys(params)) {
    const value = params[propName];
    var part = encodeURIComponent(propName) + "=";
    if (value !== null && typeof (value) !== "undefined") {
      if (typeof value === 'object') {
        for (const key of Object.keys(value)) {
          if (value[key] !== null && typeof (value[key]) !== 'undefined') {
            let params = propName + '[' + key + ']';
            var subPart = encodeURIComponent(params) + "=";
            result += subPart + encodeURIComponent(value[key]) + "&";
          }
        }
      } else {
        result += part + encodeURIComponent(value) + "&";
      }
    }
  }
  return result
}

const localMessages = {
	zh,
	en
}

export function changeLanguage(i18n, language) {
	if (!i18n.messages[language]) {
		if (!localMessages[language]) {
			localMessages[language] = en
		}
		i18n.messages[language] = localMessages[language]
	}
	return new Promise(resolve => {
		commonSystemDictionariesApi.listSystemDictionaries({
			language: language
		}).then((res) => {
			const {
				data
			} = res
			if (data) {
				data.forEach(item => {
					i18n.messages[language][item.keyName] = item.value
				})
				commonCompanyDictionariesApi.listCompanyDictionaries({
					language: language,
					companyId: storage.getCompanyId()
				}).then((res) => {
					const {
						data
					} = res
					if (data) {
						data.forEach(item => {
							i18n.messages[language][item.keyName] = item.value
						})
					}
					storage.setLanguage(language)
					i18n.locale = language
					resolve()
				})
			}
		})
	})
}
